var app = getApp();
var https = "https://www.moreclub.cn/";
var http1 = 'http://10.150.7.248:8001/'
var city;
var lat1;
var lng1;
var idn;
var page = 0;
var showbox=true
var city;
var into=false;

var getList=function(that){
  wx.request({
    url: https + 'v1/merchant/open/nearby/' + city,
    // dataType: "json",
    data: {
      pn: page,
      ps: 10,
      //location: '104.054685,30.632938'
      location: lng1 + ',' + lat1
    },
    success: function (res) {
      console.log(res)

      var resData = res.data.data
      if (resData != null) {
        //that.data.totallist.concat(respData);

        var oldlist = that.data.totallist;
        for (var i = 0; i < resData.length; i++) {
          oldlist.push(resData[i])
        }
      }
      that.setData({
        resData: resData,
        totallist:oldlist
      })
    }
  })  
}

var list = function(that,value) {
  var uid = wx.getStorageSync('infofrominput');

  if (value != '') {
    //var that = this;
    wx.request({
      url: https + 'v1/merchant/open/fullsearch?query=' + value,
      data: {
        pn: page,
        ps: 20,
        //city: chcity
        city: city
      },
      success: function (res) {
       
        console.log(res)
        if (res.data.success) {

          var listdata = res.data.data;
          if (listdata != null) {
            //that.data.totallist.concat(listdata);

            var oldlist = that.data.totallist;
            for (var i = 0; i < listdata.length; i++) {
              oldlist.push(listdata[i])
            }
          }

          that.setData({
            listdata: listdata,
            totallist: oldlist

          })

        }

      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  }
}

Page({
  data:{
    totallist:[]
  },
  onLoad:function(options){
    var that = this;
    idn = wx.getStorageSync('idn')
    city = wx.getStorageSync('city');
    lat1 = wx.getStorageSync('lat1');
    lng1 = wx.getStorageSync('lng1');
    if (city == '' || !city || typeof (city) == "undefined") {
      city = 'cd'
    }
    if (lat1 == '' || !lat1 || typeof (lat1) == "undefined") {
      lat1 = '30.632938'
    }
    if (lng1 == '' || !lng1 || typeof (lng1) == "undefined") {
      lng1 = '104.054685'
    }
    getList(that)
    
    if(idn){
      that.setData({
        idn:idn
      })
    }
    that.setData({
      showbox: false,
      into:into
    })
    if(options.all){
      that.setData({
        all:options.all
      })
    }
    
  },
  //点击搜索
  confirm:function(e){
    var that = this
    console.log('搜索',e)
    var value = e.detail.value
    var arr = that.data.totallist
    arr.splice(0)
    list(that,value)
  },
  switchMark:function(e){
    console.log(e)
    var that = this
    idn = e.currentTarget.dataset.idn
    try {
      wx.setStorageSync('idn', idn)
    } catch (e) {
    }
    var nametxt = e.currentTarget.dataset.name
    var midtxt = e.currentTarget.dataset.mid
 
     that.setData({
       idn: idn,
       showbox:false,
       into:into
     }) 
    
    //  wx.setStorage({
    //    key: "nametxt",
    //    data: nametxt
    //  })
    //  wx.setStorage({
    //    key: "midtxt",
    //    data: midtxt
    //  })

     app.globalData.nametxt = nametxt
     app.globalData.midtxt = midtxt


     wx.navigateBack({
       delta: 1
     })
    into = true
    
  },
  switchNone:function(e){
    console.log(e)
    var that = this
    idn = -1
    try {
      wx.setStorageSync('idn', idn)
    } catch (e) {
    }
    that.setData({
      showbox:true,
      into:into,
      idn:-1
    }) 
    app.globalData.nametxt = '不显示位置'
    app.globalData.midtxt == null
  
    wx.setStorage({
      key: "noname",
      data: '不显示位置'
    })
    wx.navigateBack({
      delta: 1
    })
    into = true;
  },

   onReachBottom: function () {
    // 页面上拉触底事件的处理函数
    var that = this;
    if (that.data.respData != null && that.data.respData.length != 0) {
      page += 1;
      getList(that);
    } else {
      wx.showToast({
        title: '已加载完成',
        duration: 1000
      })
    }
  }
})